$(function () {
    $("#page").load(URI + "/rbac/html/page.html");
});

//查询所有数据
function findAll() {
    //trim()去掉两边的空格
    let data = {
        username: $("#username").val(),
        sno: $("#sno").val(),
        nickName: $("#nickName").val(),
        phone: $("#phone").val(),
        pageNum: $("#pageNum").val(),
        pageSize: $("#pageSize").val()
    };
    let result = myAjax("/api/rb/user/getStudent", data, "post");
    // console.log(resultList);
    // let  result = adminLogin(resultList);
    // 总条数
    if ($.isEmptyObject(result)) {
        alert("您没有权限访问!!!")

    }
    if (result) {
        if (result.data.count != null) {
            $("#totalCount").html(result.data.count);
        }
        setData(result.data.pageInfo.list);
    }else{
        alert(result.message)
    }
}

//遍历查询到的数据
function setData(data) {
    let html = "";
    for (let i = 0; i < data.length; i++) {
        html += " <tr >\n" +
            "            <td>" + data[i].id + "</td>\n" +
            "            <td title='" + data[i].nickName + "'>" + clearNull(data[i].nickName) + "</td>\n" +
            "            <td title='" + data[i].username + "'>" + clearNull(data[i].username) + "</td>\n" +
            "            <td title='" + data[i].icon + "'><img src='" + clearNull(URI + IMG + data[i].icon) + "' style='width: 30px; height: 30px;' ></td>\n" +
            "            <td title='" + data[i].sex + "'>" + clearNull(data[i].sex) + "</td>\n" +
            "            <td title='" + data[i].age + "' >" + clearNull(data[i].age) + "</td>\n" +
            "            <td title='" + data[i].phone + "'>" + clearNull(data[i].phone) + "</td>\n" +
            "            <td title='" + data[i].email + "'>" + clearNull(data[i].email) + "</td>\n" +
            "            <td title='" + data[i].address + "'>" + clearNull(data[i].address) + "</td>\n" +
            "            <td title='" + data[i].sno + "'>" + clearNull(data[i].sno) + "</td>\n" +
            "            <td title='" + data[i].className + "'>" + clearNull(data[i].className) + "</td>\n" +
            "            <td title='" + data[i].admissionDate + "'>" + clearNull(data[i].admissionDate) + "</td>\n" +
            "            <td title='" + data[i].degree + "'>" + clearNull(data[i].degree) + "</td>\n" +
            "            <td class=\"td-status\">\n" +
            // "            <input type=\"checkbox\" name=\"switch\"  lay-text=\"开启|停用\"  checked=\"\" lay-skin=\"switch\">\n"+
            "              <span class=\"layui-btn layui-btn-normal layui-btn-mini\" onclick='isOpen(" + data[i].id + ")'>" + (clearNull(data[i].status) === 1 ? "正常" : "冻结") + "</span></td>\n" +
            "            <td class=\"td-manage\">\n" +
            "                   <button class=\"layui-btn layui-btn layui-btn-xs\"  onclick=\"toEditPage(" + data[i].id + ")\" >" +
            "                   <i class=\"layui-icon\">&#xe642;</i>编辑</button>"+
            "                   <button class=\"layui-btn-danger layui-btn layui-btn-xs\"  onclick=\"del(" + data[i].id + ")\" >" +
            "                   <i class=\"layui-icon\">&#xe642;</i>删除</button>"+
            "            </td>\n" +
            "          </tr>";
    }
    $("#tbody").html(html);
}

/***
 * 清除数据中的undefined和null将其置换为空，不显示
 */

function clearNull(value) {
    return value === "null" || value === undefined ? "" : value;
}

function toString(value) {
    if (value === 0) {
        return value === "null" || value === undefined ? "" : "目录";
    } else if (value === 1) {
        return value === "null" || value === undefined ? "" : "菜单";
    } else {
        return value === "null" || value === undefined ? "" : "按钮";
    }
}

/***
 * 删除功能
 */
function del(id) {
    let result = myAjax("/api/rb/user/del", {id: id}, "delete");
    if (result.code === 200) {
        //删除成功，就重新查询数据
        alert("删除成功(== ^^ ==)!");
        findAll();
    } else {
        alert("删除失败(╥╯^╰╥)！")
    }
}

/**
 * 编辑操作
 */
function toEditPage(id) {
    //1.拿到当前用户的id，然后将id传到编辑页面
    // 2.编辑页面加载完成后，需要通过id去数据库查询数据，
    // 3.将查询结果，显示在页面上
    //4.sessionStorage 项目开始时，只能时同一浏览器
    //5.localStorage 项目开始时，可以是不同浏览器
    sessionStorage.setItem("studentId", id);
    x_admin_show('编辑', URI + '/rbac/html/student/student-update.html', 750, 750);
}

/**
 * 状态开关
 */
function isOpen(id) {

}




